package com.gzsxy.esjy.service.core.sign.service.impl;


import com.gzsxy.esjy.common.base.exception.impl.CustomAssert;
import com.gzsxy.esjy.service.core.sign.handler.BaseSignHandler;
import com.gzsxy.esjy.sign.cache.SignCacheManager;
import com.gzsxy.esjy.sign.dto.InnerSignDto;
import com.gzsxy.esjy.sign.service.GSSignBusinessService;
import com.gzsxy.esjy.sign.service.impl.AbstractSignHandler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 实现GSSignBusinessService重写签名操作后续业务
 * @author zhangxiaolong
 * @date 2022/7/13 11:36
 */
@Service
public class GSSignServiceImpl extends AbstractSignHandler implements GSSignBusinessService {

    private String requestIdSpit = ",";
    private final Map<String, BaseSignHandler> handlers;

    public GSSignServiceImpl(ApplicationContext context, @Value("${local.ip:127.0.0.1}") String localIp,@Value("${server.port}") Integer serverPort, SignCacheManager cacheManager) {
        super();
        this.handlers = context.getBeansOfType(BaseSignHandler.class);
        super.serverPort = serverPort;
        super.signCacheManager = cacheManager;
        this.localIp = localIp;
    }

    /**
     * 签名确认
     *
     * @param signDto 签名实体
     */
    @Override
    public void signAck(InnerSignDto signDto) {

    }

    /**
     * 签名成功
     *
     * @param signDto 签名实体
     */
    @Override
    public void signSuccess(InnerSignDto signDto) {
        String[] params = signDto.getRequestId().split(requestIdSpit);
        BaseSignHandler baseSignHandler = handlers.get(params[0]);
        CustomAssert.notTrue(null != baseSignHandler, "未实现当前签名指令");
        baseSignHandler.signSuccess(signDto);
    }

    /**
     * 取消签名
     *
     * @param signDto        签名实体
     * @param isDeviceCancel 是否设备取消
     */
    @Override
    public void cancelSign(InnerSignDto signDto, boolean isDeviceCancel) {
        String[] params = signDto.getRequestId().split(requestIdSpit);
        BaseSignHandler baseSignHandler = handlers.get(params[0]);
        CustomAssert.notTrue(null != baseSignHandler, "未实现当前签名指令");
        baseSignHandler.cancelSign(signDto, isDeviceCancel);
    }

    /**
     * 设备状态处理
     * @param signDto 签名实体
     */
    @Override
    public void deviceStatus(InnerSignDto signDto) {
        String[] params = signDto.getRequestId().split(requestIdSpit);
        BaseSignHandler baseSignHandler = handlers.get(params[0]);
        CustomAssert.notTrue(null != baseSignHandler, "未实现当前签名指令");
        //设备状态处理
        baseSignHandler.deviceStatus(signDto);
    }

    @Override
    public void signRequestTimeOut(InnerSignDto signDto) {
        String[] params = signDto.getRequestId().split(requestIdSpit);
        BaseSignHandler baseSignHandler = handlers.get(params[0]);
        CustomAssert.notTrue(null != baseSignHandler, "未实现当前签名指令");
        baseSignHandler.signRequestTimeOut(signDto);
    }
}
